package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.List;

import javax.swing.JPanel;

import model.Point;


public class Graph extends JPanel implements MouseMotionListener {
	private List<Point> list;
	private int height=200, width=700;
	private int graphstart=50;
	private int mousex;
	private long starttime;
	private long endtime;
	private long span;
	private double yconv;
	private double xconv;
	
	/* inputs */
	int smoothness = 9;
	int marked_interval = 5;
	double maxpace = 10;
	
	public void setYmax(double max) {
		maxpace = max;
		yconv = height/maxpace;
	}
		
	public Graph() {
		list = null;
		this.addMouseMotionListener(this);
		this.setSize(width, height);
	}
	
	public void show(List<Point> list) {
		mousex = 0;
		
		this.list = list;
		starttime = list.get(0).time.getTime();
		endtime = list.get(list.size()-1).time.getTime();
		span = endtime-starttime;	
		
		yconv = height/maxpace;
		xconv = (double)width/span;
		System.out.println("xconv: "+xconv);
	}
	
	public int getX(int index) {
		long reltime = list.get(index).time.getTime() - starttime;
		int xval = (int) (reltime * xconv) + graphstart;
		
		return xval;
	}
	
	@Override
	public void paintComponent(Graphics g) {


		g.clearRect(0,0, getWidth(), getHeight());
		g.setColor(new Color(50, 50, 50));
		g.fillRect(graphstart, 0, width, height);
		
		if (mousex > 0) {
			g.setColor(Color.GRAY);
			g.drawLine(mousex, 0, mousex, height);
			
			for (int i=0; i<list.size(); ++i) {
				if (getX(i) > mousex) {
					double pace = smoothPace(i);
					g.drawString(format_pace(pace) +"min/km", mousex+2, 15);
					break;
				}
			}
		}
		
		g.setColor(Color.RED);
		if (list != null) {
//			for (int i=0; i<list.size()-1; ++i) {
//				
//				double pace = smoothPace(i, smoothness, list);
//				
//				if (maxpace < pace)
//					maxpace = pace;
//			}
			

			
			int prevx=graphstart, prevy=height-(int) (smoothPace(0)*yconv);
			for (int i=0; i<list.size(); ++i) {
				Point p = list.get(i);

				double pace = smoothPace(i, smoothness, list);
				
				int newx = (int) ((p.time.getTime() - starttime) * xconv + graphstart);
				int newy = height-(int) (pace*yconv);
				
				g.drawLine(prevx, prevy, newx, newy);
				
				prevx=newx;
				prevy=newy;
			}
			
			
			g.drawString("pace ", 0, height/2);
			g.drawString(""+maxpace, 0, 15);
			for (int i=0; true; ++i) {
				int newx = (int) (1000*60*marked_interval*i * xconv + graphstart);
				if (newx > graphstart+width) {
					break;
				}
				
				g.drawString("|5min", newx, height+15);
			}
		}
	}
	
	public double smoothPace(int index) {
		return smoothPace(index, smoothness, list);
	}
	
	public double smoothPace(int index, int smoothness, List<Point> points) {
		int num_paces = 0;
		double total_pace = 0;
		for (int j=index-smoothness; j<=index+smoothness; ++j) {

			if (j < 0 || j == index || j >= points.size())
				continue;
			
			total_pace += points.get(index).paceTo(points.get(j));
			++num_paces;
		}
		
		double pace = total_pace/num_paces;
		return pace;
	}

	@Override
	public void mouseDragged(MouseEvent me) {
		mousex = me.getX();
		if (mousex < graphstart || mousex > graphstart+width) {			
			mousex = 0;
		}
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {}
	
	public String format_pace(double pace) {
		String s = String.format("%.2f", pace);
		return s;
	}


}
